package group32.client;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.XMLParser;

public class XMLandJSON {
	/**
	 * Parse XML, return root element (not document root)
	 * 
	 * @param result
	 * @return
	 */
	public static Node parseXML(final String result) {
		try {
			System.out.println(result);
			Document d = XMLParser.parse(result);
			for (Node n = d.getFirstChild(); n != null; n = n.getNextSibling()) {
				if (n.getNodeType() == Node.ELEMENT_NODE) {
					return n;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Converts the XML representation of the user into a JSON
	 * representation for the JavaScript Infovis Toolkit
	 * 
	 * @param doc
	 * @return
	 */
	public static JSONObject getJsonForUser(final Node doc) {
		String name = "";
		String utype="";
		int id = 0;

		JSONArray circles = new JSONArray();
		
		JSONObject data = new JSONObject();
		JSONObject cdata = new JSONObject();
		
		for (Node child = doc.getFirstChild(); child != null; child = child.getNextSibling()) {
			if (child.getNodeName().equals("id")) {
				id = Integer.valueOf(getTextNode(child));
			} 
			else if (child.getNodeName().equals("name")) {
				name = (getTextNode(child));
			}
			else if (child.getNodeName().equals("hasFriend")) {
				for (Node circle = child.getFirstChild(); circle != null; circle = circle.getNextSibling()) {
					 if (circle.getNodeName().equals("friend")) {
						int fid = 0;
						String fname = "";
						String ftype = "";
						String flogon = "";
						for (Node friendInfo = circle.getFirstChild(); friendInfo != null; friendInfo = friendInfo.getNextSibling()) {
							if (friendInfo.getNodeType() == Node.TEXT_NODE) {
								
							} else if (friendInfo.getNodeName().equals("id")) {
								fid = Integer.valueOf(getTextNode(friendInfo));
							} else if (friendInfo.getNodeName().equals("name")) {
								fname = getTextNode(friendInfo);
							}
							else if (friendInfo.getNodeName().equals("ftype")) {
								ftype = getTextNode(friendInfo);
							}
							else if (friendInfo.getNodeName().equals("funame")) {
								flogon = getTextNode(friendInfo);
							}
						}
						JSONObject thisFriend = new JSONObject();
						
						thisFriend.put("id", new JSONNumber(fid));
						thisFriend.put("name", new JSONString(fname));
						cdata.put("usertype", new JSONString(ftype));
						cdata.put("username", new JSONString(flogon));
						thisFriend.put("data",  cdata);
						circles.set(circles.size(), thisFriend);
					}
				}
			}
			else if (child.getNodeName().equals("type")) {
				data.put("usertype", new JSONString(getTextNode(child)));
			}
			else if (child.getNodeName().equals("uname")) {
				data.put("username", new JSONString(getTextNode(child)));
			}
		}
		
		JSONObject obj = new JSONObject();
		
		obj.put("id", new JSONNumber(id));
		obj.put("name", new JSONString(name));
		obj.put("data", data);
		obj.put("children", circles);
	
		System.out.println(obj.toString());
		return obj;
	}


	/**
	 * Retrieves the text node child of an XML element node
	 * 
	 * @param parent
	 * @return
	 */
	public static String getTextNode(final Node parent) {
		Node child = parent.getFirstChild();
		
		while (child != null && child.getNodeType() != Node.TEXT_NODE)
			child = child.getNextSibling();
		
		if (child != null)
			return child.getNodeValue();
		else
			return null;
	}
	

}
